package app.kisslinux.cn.utilsforlin.ui.activity.cutting_plates;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

import com.blankj.utilcode.util.StringUtils;

import app.kisslinux.cn.utilsforlin.R;
import app.kisslinux.cn.utilsforlin.ui.dialog.DialogFactory;
import app.kisslinux.cn.utilsforlin.ui.view.CuttingPlatesExampleView;

public class CuttingPlatesActivity extends AppCompatActivity {


    private EditText etHeight;
    private EditText etBottomLength;
    private EditText etPlatesSize;
    private CuttingPlatesExampleView cuttingPlatesExample;

    public static Intent createIntent(Context context) {
        return new Intent(context, CuttingPlatesActivity.class);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cutting_plates);

        etHeight = findViewById(R.id.et_height);
        etBottomLength = findViewById(R.id.et_bottom_length);
        etPlatesSize = findViewById(R.id.et_plates_size);
        cuttingPlatesExample = findViewById(R.id.cutting_plates_example);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.cutting_plates_activity, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_submit: {
                String plateSize = etPlatesSize.getText().toString().trim();
                String bottomLength = etBottomLength.getText().toString().trim();
                String height = etHeight.getText().toString().trim();

                if (validateInput(plateSize, bottomLength, height)) {
                    cuttingPlatesExample.updatePlates(Integer.valueOf(plateSize));
                    cuttingPlatesExample.updateProduct(Integer.valueOf(bottomLength), Integer.valueOf(height));
                    cuttingPlatesExample.invalidate();
                }
                return true;
            }
            case R.id.menu_error: {
                DialogFactory.instance().createSimpleDialog(CuttingPlatesActivity.this,
                        "计算错误？",
                        "有任何问题，你给我打电话，我好改正。").show();
                return true;
            }
            default: {
                return super.onOptionsItemSelected(item);
            }
        }
    }

    private boolean validateInput(String plateSize, String bottomLength, String height) {
        // check input is not empty.
        if (StringUtils.isEmpty(plateSize)) {
            DialogFactory.instance().createSimpleDialog(CuttingPlatesActivity.this,
                    "提醒",
                    "你得输入板材的宽度").show();
            return false;
        }

        // check input is not empty.
        if (StringUtils.isEmpty(bottomLength)) {
            DialogFactory.instance().createSimpleDialog(CuttingPlatesActivity.this,
                    "提醒",
                    "你得输入下口长度").show();
            return false;
        }

        // check input is not empty.
        if (StringUtils.isEmpty(height)) {
            DialogFactory.instance().createSimpleDialog(CuttingPlatesActivity.this,
                    "提醒",
                    "你得输入制作高度").show();
            return false;
        }

        return true;

    }
}
